﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using net8.Models;
using net8.Models.ShangPin;
using net8.Models.Upload;
using net8.Models.XiTong;
using net8.Views;
using SqlSugar;

namespace net8.Controllers
{
    /// <summary>
    /// 商品管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ShangPinController : ControllerBase
    {
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public PageReturn<ShangPinView> GetPageList(int pageIndex, int pageSize)
        {
            int total = 0;
            var list = DbHelper.Db.Queryable<SpShangPin>()
                .Includes(s => s.Image)
                .Includes(s => s.DetailList, r => r.File)
                .Select(s => new ShangPinView()
                {
                    Id = s.Id,
                    Name = s.Name,
                    Price = s.Price,
                    Image = new UploadFile() { Id = s.FileId, Url = s.Image.FilePath },
                    DetailList = s.DetailList.Select(r => new UploadFile() { Id = r.FileId, Url = r.File == null ? "" : r.File.FilePath }).ToList()
                }).ToPageList(pageIndex, pageSize, ref total);

            return new PageReturn<ShangPinView> { Total = total, List = list };
        }

        [HttpPost]
        public void Add([FromBody] ShangPinView model)
        {
            DateTime current = DateTime.Now;
            SpShangPin sp = new SpShangPin()
            {
                Id = Guid.NewGuid(),
                AddTime = current,
                UpdateTime = current,
                IsDel = false,
                Name = model.Name,
                Price = model.Price,
                FileId = model.Image?.Id ?? Guid.Empty,
                DetailList = new List<UpRelation>()
            };

            foreach (UploadFile item in model.DetailList)
            {
                sp.DetailList.Add(new UpRelation()
                {
                    Id = Guid.NewGuid(),
                    AddTime = current,
                    UpdateTime = current,
                    IsDel = false,
                    Type = "",
                    TableId = sp.Id,
                    FileId = item.Id
                });
            }

            DbHelper.Db.InsertNav(sp).Include(s => s.DetailList).ExecuteCommand();
        }

        [HttpPost]
        public void Update([FromBody] ShangPinView model)
        {
            DateTime current = DateTime.Now;
            SpShangPin sp = new SpShangPin()
            {
                Id = model.Id,
                AddTime = current,
                UpdateTime = current,
                IsDel = false,
                Name = model.Name,
                Price = model.Price,
                FileId = model.Image?.Id ?? Guid.Empty,
                DetailList = new List<UpRelation>()
            };

            foreach (UploadFile item in model.DetailList)
            {
                sp.DetailList.Add(new UpRelation()
                {
                    Id = Guid.NewGuid(),
                    AddTime = current,
                    UpdateTime = current,
                    IsDel = false,
                    Type = "",
                    TableId = sp.Id,
                    FileId = item.Id
                });
            }

            DbHelper.Db.UpdateNav(sp).Include(s => s.DetailList).ExecuteCommand();
        }

        [HttpDelete]
        public void Delete(Guid id)
        {
            DbHelper.Db.DeleteNav<SpShangPin>(s => s.Id == id).Include(s => s.DetailList).ExecuteCommand();
        }
    }
}
